/* $Id: IconHelper.java 56 2010-01-23 14:49:04Z aaron.day $ */

package org.crazydays.gameplan.util;


import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * IconHelper
 */
public class IconHelper
{
    /** logger */
    protected static Logger LOGGER = LoggerFactory.getLogger(IconHelper.class);

    /**
     * Load icon from resource within the classpath.
     * 
     * @param resource Resource
     * @return Icon
     */
    public static Icon loadIcon(String resource)
    {
        if (resource == null) {
            return null;
        }

        InputStream inputStream =
            IconHelper.class.getResourceAsStream(resource);
        if (inputStream == null) {
            LOGGER.warn("Unable to load icon {}", resource);
            return null;
        }

        try {
            return new ImageIcon(ImageIO.read(inputStream));
        } catch (IOException e) {
            LOGGER.warn("Unable to load icon {}", resource, e);
            return null;
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                LOGGER.warn("Error closing inputStream", e);
            }
        }
    }
}
